package com.cym.controller.adminPage;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.cym.model.Bak;
import com.cym.model.BakSub;
import com.cym.service.BakService;
import com.cym.service.SettingService;
import com.cym.sqlhelper.bean.Page;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.ModelAndView;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Mapping("/adminPage/bak")
public class BakController extends BaseController {
    @Inject
    SettingService settingService;
    @Inject
    BakService bakService;

    @Mapping
    public ModelAndView index(ModelAndView modelAndView, Page page) {
        page = bakService.getList(page);

        modelAndView.put("page", page);
        modelAndView.view("/adminPage/bak/index.html");
        return modelAndView;
    }

    /**
     * 获取对比数据
     *
     * @param id
     * @return
     */
    @Mapping("getCompare")
    public JsonResult getCompare(String id) {
        // TODO 有子文件时需要将子文件一起查出来
        Bak bak = sqlHelper.findById(id, Bak.class);
        Bak pre = bakService.getPre(id);
        if (pre == null) {
            return renderError("没有更早的备份文件");
        }

        Map<String, Bak> map = new HashMap<>(2);
        map.put("bak", bak);
        map.put("pre", pre);

        return renderSuccess(map);
    }

    /**
     * 获取当前配置信息对比数据
     *
     * @param id
     * @return
     */
    @Mapping("getCurrentCompare")
    public JsonResult getCurrentCompare(String id) {
        Bak bak = sqlHelper.findById(id, Bak.class);
        // 获取当前配置信息，和当前配置对比
        Bak pre = new Bak();
        String nginxPath = settingService.get("nginxPath");
        String conf = FileUtil.readString(nginxPath, StandardCharsets.UTF_8);
        pre.setContent(conf);
        pre.setTime("当前版本");
        Map<String, Bak> map = new HashMap<>(2);
        map.put("bak", bak);
        map.put("current", pre);

        return renderSuccess(map);
    }

    @Mapping("content")
    public JsonResult content(String id) {
        Bak bak = sqlHelper.findById(id, Bak.class);
        return renderSuccess(bak);
    }

    @Mapping("replace")
    public JsonResult replace(String id) {
        Bak bak = sqlHelper.findById(id, Bak.class);

        String nginxPath = settingService.get("nginxPath");

        if (StrUtil.isNotEmpty(nginxPath)) {
            File pathFile = new File(nginxPath);
            // 写入主文件
            FileUtil.writeString(bak.getContent(), pathFile, StandardCharsets.UTF_8);

            // 写入子文件
            String confd = pathFile.getParent() + File.separator + "conf.d" + File.separator;
            FileUtil.del(confd);
            FileUtil.mkdir(confd);

            List<BakSub> subList = bakService.getSubList(bak.getId());
            for (BakSub bakSub : subList) {
                FileUtil.writeString(bakSub.getContent(), confd + bakSub.getName(), StandardCharsets.UTF_8);
            }
            return renderSuccess();
        } else {
            return renderError(m.get("bakStr.pathNotice"));
        }

    }


    @Mapping("del")
    public JsonResult del(String id) {
        bakService.del(id);
        return renderSuccess();
    }

    @Mapping("delAll")
    public JsonResult delAll() {
        bakService.delAll();

        return renderSuccess();
    }

}
